var xsps = {
    setBtnStatus: function () {
        var row = $('#grid_xsps').datagrid('getSelected');
        if (row && row.id && row.psdh == null) {
            $("#btn_xsps_add").show();
            $("#btn_xsps_mod").hide();
            $("#btn_xsps_print").hide();
            $("#btn_xsps_del").hide();
        } else if (row && row.id && row.psdh != null) {
            $("#btn_xsps_add").hide();
            $("#btn_xsps_mod").show();
            $("#btn_xsps_print").show();
            $("#btn_xsps_del").show();
        } else {
            $("#btn_xsps_add").hide();
            $("#btn_xsps_mod").hide();
            $("#btn_xsps_print").hide();
            $("#btn_xsps_del").hide();
        }
    }
}
$(function () {
    //加载查询条件中的所属公司下拉框
    $("[name='xsps_sbgs']").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });

    $("#xsps_kh").qyjgzcombobox();

    //加载仓库
    $('#xsps_thck').combobox({
        url: $.webPath() + '/organ/ck/ckmcList',
        valueField: 'id',
        textField: 'mc',
        panelHeight: '200'
    });

    //表格初始加载
    $('#grid_xsps').datagrid({
        url: $.webPath() + '/xsgl/xsdgl/xsdXspsList',
        toolbar: "#toolbar_xsps",
        fitColumns: false,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        striped: true,
        frozenColumns: [
            [
                {field: 'xsdh', title: '销售单号', align: 'center', width: 150, formatter: xspsCell},
                {field: 'dateCreated', title: '开单日期', align: 'center', width: 70},
                {field: 'xslx', title: '销售类型', align: 'center', width: 55},
                {field: 'yps', title: '配送状态', align: 'center', width: 55, formatter: function (value, rowData) {
                    return value == "1" ? '<a>已配送</a>' : '<a style="color:red">未配送</a>';
                }}
            ]
        ],
        columns: [
            [
                {field: 'psdh', title: '配送单号', align: 'center', width: 150},
                {field: 'kh_qc', title: '客户名称', align: 'center', width: 150},
                {field: 'gc_mc', title: '工程名称', align: 'center', width: 150},
                {field: 'jss', title: '交货地址/收货人/电话', align: 'center', width: 300},
                {field: 'jhsj', title: '交货日期', align: 'center', width: 100},
                {field: 'xssl', title: '开单数量', align: 'center', width: 100},
                {field: 'cksl', title: '出库数量', align: 'center', width: 100},
                {field: 'thck_mc', title: '提货仓库', align: 'center', width: 150},
                {field: 'psfy', title: '配送费用', align: 'center', width: 200,
                    formatter: function (value) {
                        return formatJeForHtml(value);
                    }},
                {field: 'createUser_xm', title: '制单人', align: 'center', width: 100},
                {field: 'xsgs_qc', title: '公司名称', align: 'center', width: 150},
                {field: 'wls_qc', title: '物流商', align: 'center', width: 250},
                {field: 'shch', title: '送货车号', align: 'center', width: 200},
                {field: 'jsy', title: '驾驶员', align: 'center', width: 200},
                {field: 'lxdh', title: '联系电话', align: 'center', width: 100}
            ]
        ],
        onLoadSuccess: function () {
            xsps.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow: function () {
            xsps.setBtnStatus();
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.xslx) {
                    this.xslx = this.xslx == 'htxs' ? "合同销售" : this.xslx == 'zddh' ? "直达到货" : this.xslx == 'bgxs' ? "保供销售" : "库存零售";
                }
            })
            return data;
        }
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_xsps_add").click(function () {
        xsps_add('新增销售配送单', 'add_xsps');
    });
    //修改按钮
    $("#btn_xsps_mod").click(function () {
        xsps_modify('修改销售配送单', 'update_xsps');
    });
    //打印
    $("#btn_xsps_print").click(function () {
        xsps_print('打印销售配送单', 'print_xsps');
    });
    //删除按钮
    $("#btn_xsps_del").click(function () {
        xsps_delete();
    });
    //查询按钮
    $("#btn_xsps_search").click(function () {
        xsps_search();
    });
    //双击事件
//    $("#grid_xsps").datagrid({
//        onDblClickCell:function (index, field) {
//            $('#grid_xsps').datagrid('selectRow', index);
//            var row = $('#grid_xsps').datagrid('getSelected');
//            if (field == "xsdh") {
//                $("#btn_xsps_mod:visible").click();
//            }
//        }
//    });
});


//新增销售配送单
function xsps_add(title, action) {
    var record = $("#grid_xsps").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要配送的销售单!", "warning");
        return;
    }
    var href = $.webPath() + "/wlgl/xsps/xsps_content?xsdId=" + record.id;
    var actionUrl = $.webPath() + "/wlgl/xsps/add_xsps";
    xsps_showDialog(title, href, actionUrl);
}

//修改销售单配送
function xsps_modify(title, action) {
    var record = $("#grid_xsps").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
    var href = $.webPath() + "/wlgl/xsps/xsps_content?xsdId=" + record.id + "&&xspsId=" + record.psd_id;
    var actionUrl = $.webPath() + "/wlgl/xsps/" + action;
    xsps_showDialog(title, href, actionUrl);
}

//销售单配送
function xsps_print(title, action) {
    var record = $("#grid_xsps").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要打印的记录!", "warning");
        return;
    }
    var href = $.webPath() + "/wlgl/xsps/xsps_content_print?xsdId=" + record.id + "&&xspsId=" + record.psd_id;
    var actionUrl = $.webPath() + "/wlgl/xsps/" + action;
    xsps_showPrintDialog(title, href, actionUrl);
}

function xsps_showPrintDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 1024,
        height: 500,
        cache: false,
        url: href,
        modal: true,
        maximizable: true
    });
}

//弹出新增或修改的窗口
function xsps_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 750,
        height: 430,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_xsps').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_xsps').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除销售结算单
function xsps_delete() {
    var row = $('#grid_xsps').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该销售配送单？', function (r) {
            if (r) {
                var index = $('#grid_xsps').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/wlgl/xsps/delete_xsps",
                    dataType: 'json',
                    type: 'POST',
                    data: {xspsId: row.psd_id},
                    beforeSend: function () {
                        $("#layout_xsps").mask();
                    },
                    success: function (data) {
                        $("#layout_xsps").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_xsps').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的销售配送单！", "warning");
    }
}

function xsps_search() {
    var sbgs = $("#toolbar_xsps input[name='xsps_sbgs']").val();
    var thckId = $('#xsps_thck').combotree('getValue') == null ? null : $('#xsps_thck').combotree('getValue');
    $('#grid_xsps').datagrid('load',
        {
            thckId: thckId,
            xspsKh: $('#xsps_kh').attr("_text") == null ? null : $('#xsps_kh').attr("_text"),
            sbgsId: sbgs,
            pszt: $('#xsps_pszt').combobox('getValue'),
            xsdh: $('#xsps_xsdh').val(),
            psdh: $('#xsps_psdh').val()
        }
    );
}
